#coding:utf8
import tkinter as tk
from tkinter.filedialog import askopenfilename
from sen_word.word import fenci
from tkinter import messagebox
import os

class To_words(tk.Frame):
    
    def __init__(self, master=None):
        tk.Frame.__init__(self,master)
        self.openfilename = ""
        self.grid()
        self.createWidgets()    
    
    def createWidgets(self):
        self.lb = tk.Label(self,text = '请选择文件',width="58")
        self.lb.grid(row=0,column=0)
        self.btfile = tk.Button(self,text="选择文件",width="20",command=self.sentence)
        self.btfile.grid(row=1,column=0)
        self.btstart = tk.Button(self,text="开始分词",width="20",command=self.sen_to_words)   
        self.btstart.grid(row=2,column=0)
        
    def sentence(self):
        data_dir = os.path.dirname(os.path.dirname(os.getcwd()))+'\\data'
        self.openfilename = askopenfilename(initialdir = data_dir,title = "选择文件",filetypes = (("csv files","*.csv"),("all files","*.*")))
        if self.openfilename != '':
            self.lb.config(text = "您选择的文件是："+self.openfilename)

        else:
            self.lb.config(text = "您没有选择任何文件")   
    
    def sen_to_words(self):
        
        if self.openfilename.strip() == "":
            messagebox.showinfo("提示", "你没有选择文件！")
        if self.openfilename.strip() != "": 
            fenci(self.openfilename)   
            messagebox.showinfo("提示", "分词成功！")
    
 
